﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace LightSwitchApplication
{
    public abstract class BasicController : ApiController
    {
        //public virtual object Get();
        //public virtual object Get() //IEnumerable<string>
        //{
        //    return new object();
        //}
        //public virtual object Get(int id);
        //public virtual object Get(int id)
        //{
        //    return new object();
        //}

        //public virtual void Post([FromBody]string value);
        //// POST api/<controller>
        //public virtual void Post([FromBody]string value)
        //{
        //}

        //public virtual void Put(int id, [FromBody]string value);
        //// PUT api/<controller>/5
        //public virtual void Put(int id, [FromBody]string value)
        //{
        //}

        //public virtual void Delete(int id);
        //// DELETE api/<controller>/5
        //public virtual void Delete(int id)
        //{
        //}

        private bool ownServerContext;
        /// <summary>
        /// Returns the data workspace for the server context
        /// </summary>
        protected LightSwitchApplication.DataWorkspace DataWorkspace
        {
            get
            {
                // The server context is automatically cached in the "Current" property
                if (ServerApplicationContext.Current == null)
                {
                    this.ownServerContext = true;
                    ServerApplicationContext.CreateContext();

                    if (!ServerApplicationContext.Current.Application.User.IsAuthenticated)
                    {
                        throw new MemberAccessException("User is not Authenticated");
                    }
                }
                else
                {
                    this.ownServerContext = false;
                }
                return ServerApplicationContext.Current.DataWorkspace;
            }
        }
        /*protected LightSwitchApplication.Application Application
        {
            get
            {
                // The server context is automatically cached in the "Current" property
                if (ServerApplicationContext.Current == null)
                {
                    this.ownServerContext = true;
                    ServerApplicationContext.CreateContext();
                }
                else
                {
                    this.ownServerContext = false;
                }
                return ServerApplicationContext.Current.Application;
            }
        }*/

        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing)
                {
                    if (this.ownServerContext &&
                        ServerApplicationContext.Current != null &&
                       !ServerApplicationContext.Current.IsDisposed)
                    {
                        ServerApplicationContext.Current.Dispose();
                    }
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }
    }
}